package com.combetter.changxing.ui.dialog;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.combetter.changxing.R;
import com.combetter.changxing.databinding.DialogBelowSelectBinding;
import com.combetter.changxing.util.DeviceUtils;
import com.combetter.changxing.util.ResUtils;

import java.util.ArrayList;

import static com.combetter.changxing.constanst.Constants.REUEST_CODDE;

/**
 * 单选dialog
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/15.
 */
public class BelowSelectDialog extends DialogFragment {
    public static String REQUESE = "title";
    public static String REQUESEID = "titleId";
    private DialogBelowSelectBinding selectBinding;
    private ArrayList<String> mOptionsItems = new ArrayList<>();
    private int titleId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        selectBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_below_select, container, false);
        getDialog().setCanceledOnTouchOutside(false); // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        window.setAttributes(lp);

        slideToUp(selectBinding.getRoot());

        initVies();
        setWheeLView();
        return selectBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        int width = (int) DeviceUtils.getScreenWidth(getContext());
        int height = ResUtils.getDimens(getContext(), R.dimen.DIMEN_450PX);
        getDialog().getWindow().setLayout(width, height);
    }

    /**
     * 初始化控件
     */
    private void initVies() {
        mOptionsItems = getArguments().getStringArrayList("mOptionsItems");
        titleId = getArguments().getInt("titleId");
        selectBinding.ivCancel.setOnClickListener(v -> dismiss());
    }

    private void setWheeLView() {
        selectBinding.wheelview.setCyclic(false);
        selectBinding.wheelview.setAdapter(new ArrayWheelAdapter(mOptionsItems));

        selectBinding.tvConfirm.setOnClickListener(v -> {
            String title = mOptionsItems.get(selectBinding.wheelview.getCurrentItem());
            Intent intent = new Intent();
            intent.putExtra(REQUESE, title);
            intent.putExtra(REQUESEID, titleId);
            //获得目标Fragment,并将数据通过onActivityResult放入到intent中进行传值
            getTargetFragment().onActivityResult(REUEST_CODDE, Activity.RESULT_OK, intent);
            dismiss();
        });
    }

    public static void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


}
